<?php
defined('SYSPATH') or die('No direct script access.');

class Controller_Request extends Controller_Initialize {

  public $secure_actions = array(
      );

  public function action_index() {

    $this->template->content = View::factory('templates/request/index');

  }

  public function action_view($pattern) {

    $request = ORM::factory('request')->where('pattern', '=', $pattern)->find();
    $comments = ORM::factory('comments_request')->where('pattern', '=', $pattern)->find_all();
    $request_vote = ORM::factory('request_vote')->where('pattern', '=', $pattern)->where('username', '=', $this->userAuth->username)->find();
    $this->template->content = View::factory('templates/request/view')
            ->bind('request', $request)
            ->bind('request_vote', $request_vote)
            ->bind('comments', $comments);

  }

  public function action_add() {

    $view = View::factory('templates/request/index');

    if (!empty($_POST)) {
      $file_name = explode(",", $_POST['src']);

      $request = null;
      $request = ORM::factory('request');
      $post = $request->validate_add($_POST);

      if ($post->check()) {

        $add_date = Time();        
        $request->username = $post['nick'];
        $request->email = $post['email'];
        $request->about = $post['about'];
        $request->fullname = $post['name'];
        $request->pattern = $post['pattern'];
        $request->date = $add_date;
        $request->save();

        if ($request->saved()) {

          // file upload position
          $file_count = 1;

          foreach ($file_name as $file) {

            $photo = Image::factory('temp/' . $file);

            if (!is_dir(DOCROOT . '/files/requests/' . $add_date)) {
              mkdir(DOCROOT . '/files/requests/' . $add_date, 0755, true);
            }

            if ($photo->width > 680) {
              $photo->resize(680, NULL);
            }
            $photo->save(DOCROOT . '/files/requests/' . $add_date . '/' . $add_date . '_' . $file_count . '.jpg');
            $file_count++;
          }
          
          Message::add('success', '<strong>Vaše žádost byla úspěšně odeslána pod číslem "'.$request->pattern.'".</strong>');
          Request::instance()->redirect('request/view/'.$request->pattern);

        } else {
          Message::add('warning', '<strong>Omlouváme se ale nastala chyba při ukládání do databáze.</strong>');
        }

      } else {

        $informations = null;
        foreach ($post->errors('validate') as $item) {
          $informations .= '<span>' . $item . '</span>';
        }
        
        Message::add('warning', '<strong>Vaše údaje nebyly správně vyplněny.</strong>'.$informations);
        $view->set('dataPost', $_POST);

      }
    }

    $this->template->content = $view;

  }

  public function action_vote() {

    if (!empty($_POST)) {

      $request_vote = ORM::factory('request_vote')->where('pattern', '=', $_POST['rid'])->where('username', '=', $this->userAuth->username)->find();

      if ($request_vote->loaded()) {
        $request_vote->vote = $_POST['vote'];
      } else {
        $request_vote = null;
        $request_vote = ORM::factory('request_vote');
        $request_vote->username = $this->userAuth->username;
        $request_vote->pattern = $_POST['rid'];
        if ($_POST['vote'] > 1) {
          $request_vote->vote = 2;
        } else {
          $request_vote->vote = $_POST['vote'];
        }
      }
      $request_vote->save();

      if ($request_vote->saved()) {
        Message::add('success', '<strong>Vaše hodnocení bylo zaznamenáno.</strong>');
      } else {
        Message::add('success', '<strong>Vaše hodnocení nebylo zaznamenáno.</strong>');
      }
    }

    Request::instance()->redirect('request/view/' . $request_vote->pattern);
  }

  public function after() {

    if ($this->auto_render) {

      $styles = array('media/js/uploadify/uploadify.css' => 'screen','media/js/jcrop/jquery.jcrop.css' => 'screen');
      $this->template->styles = array_reverse(array_merge($this->template->styles, $styles));
      $scripts = array('media/js/uploadify/jquery.uploadify.v2.1.4.min.js', 'media/js/uploadify/swfobject.js', 'media/js/jcrop/jquery.jcrop.min.js');
      $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts));
    }

    // Run anything that needs to run after this.
    parent::after();
  }
  
 }